package com.cango.qutz.web.auth.position.controller;

import com.cango.qutz.web.auth.org.service.IOrgService;
import com.cango.qutz.web.entity.OrgEntity;
import com.cango.framework.base.AjaxReturnVO;
import com.cango.framework.base.pagination.DataTableRequest;
import com.cango.framework.base.pagination.DataTableResponse;
import com.cango.framework.base.pagination.PageInfo;
import com.cango.framework.base.pagination.PaginationResult;
import com.cango.qutz.web.auth.position.service.IPositionService;
import com.cango.qutz.web.entity.PositionEntity;
import com.cango.qutz.web.entity.PositionRoleEntity;
import com.cango.framework.base.controller.BaseController;
import com.cango.qutz.web.auth.org.service.IOrgService;
import com.cango.qutz.web.auth.position.service.IPositionService;
import com.cango.qutz.web.entity.OrgEntity;
import com.cango.qutz.web.entity.PositionEntity;
import com.cango.qutz.web.entity.PositionRoleEntity;
import com.google.gson.Gson;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * ClassName: PositionController
 * </p>
 * <p>
 * Description: 岗位Controller
 * </p>
 * <p>
 * Author: weih
 * </p>
 * <p>
 * Date: 2015-04-01
 * </p>
 */
@Controller
@RequestMapping("/auth/position")
public class PositionController extends BaseController {

    /**
     * log
     */
    private final Log log = LogFactory.getLog(PositionController.class);

    /**
     * <p>
     * Field positionService: 岗位Service注入
     * </p>
     */
    @Autowired
    private IPositionService positionService;

    /**
     * <p>
     * Field orgService: 机构Service注入
     * </p>
     */
    @Autowired
    private IOrgService orgService;

    /**
     * 功能描述: <br>
     * 到岗位管理页面
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/toPosManage")
    public String toUserManage() {
        return "/position/positionManage";
    }

    @RequestMapping("/toPosList")
    public String toOrgList(String orgCode, Model model) {
        // 设置父节点代码
        model.addAttribute("orgCode", orgCode);
        return "/position/positionList";
    }

    /**
     * <p>
     * Description: 分页查询岗位记录
     * </p>
     * 
     * @param position 岗位对象
     * @param response response
     * @param dataTable 分页对象
     */
    @RequestMapping("/getPosListData")
    @ResponseBody
    public DataTableResponse<PositionEntity> getListData(PositionEntity position, HttpServletResponse response,
                                                         DataTableRequest dataTable) {
        PageInfo pageInfo = new PageInfo(dataTable.getCurrentPage(), dataTable.getiDisplayLength());
        PaginationResult<PositionEntity> result = positionService.queryListByPage(position, pageInfo);
        return new DataTableResponse<>(dataTable.getsEcho(), result);
    }

    /**
     * <p>
     * Description: 跳转岗位新增页面
     * </p>
     */
    @RequestMapping("/toAddPos")
    public String toAddPosition(String orgCode, Model model) {
        OrgEntity organ = this.orgService.queryOrgByCode(orgCode);
        model.addAttribute("organ", organ);
        return "position/positionAdd";
    }

    /**
     * 功能描述: <br>
     * 验证岗位代码的唯一性
     *
     * @param positionCode String
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/validCode")
    @ResponseBody
    public boolean validCode(String positionCode) {
        return this.positionService.validCode(positionCode);
    }

    /**
     * <p>
     * Description: 保存岗位记录
     * </p>
     * 
     * @param position 岗位对象
     * @param response
     */
    @RequestMapping("/savePosition")
    public void savePosition(PositionEntity position, HttpServletResponse response) {
        position.setCreateUser(getSessionUser().getUserId());
        position.setCreateTime(new Date());
        positionService.insert(position);
        log.info("用户" + getSessionUser().getUserName() + "新增了岗位:" + (new Gson()).toJson(position));
        super.ajaxJson(response, AjaxReturnVO.success());
    }

    /**
     * 功能描述: <br>
     * 新增岗位角色关系
     *
     * @param positionCode
     * @param params
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/savePositionRole")
    public void savePositionRole(String positionCode, String params, HttpServletResponse response) {
        PositionRoleEntity posRole = new PositionRoleEntity();
        posRole.setPositionCode(positionCode);
        posRole.setRoleCode(params);
        posRole.setCreateUser(this.getSessionUser().getUserId());
        this.positionService.savePositionRole(posRole);
        log.info("用户" + getSessionUser().getUserName() + "新增了岗位:" + (new Gson()).toJson(posRole));
        super.ajaxJson(response, AjaxReturnVO.success());
    }

    /**
     * <p>
     * Description: 跳转岗位编辑页面
     * </p>
     *
     * @param positionId 岗位对象
     * @param model
     * @return
     */
    @RequestMapping("/toEditPosition")
    public String toEditPosition(Integer positionId, Model model) {
        PositionEntity posEntity = this.positionService.queryObjectById(positionId);
        model.addAttribute("posEntity", posEntity);
        OrgEntity organ = this.orgService.queryOrgByCode(posEntity.getOrgCode());
        model.addAttribute("organ", organ);
        return "position/positionEdit";
    }

    /**
     * <p>
     * Description: 保存岗位修改记录
     * </p>
     * 
     * @param position 岗位对象
     * @param response
     */
    @RequestMapping("/editPosition")
    public void editPosition(PositionEntity position, HttpServletResponse response) {
        position.setUpdateUser(getSessionUser().getUserId());
        positionService.update(position);
        log.info("用户" + getSessionUser().getUserName() + "修改了岗位:" + (new Gson()).toJson(position));
        super.ajaxJson(response, AjaxReturnVO.success());
    }

    /**
     * <p>
     * Description: 新增完成跳转岗位编辑页面
     * </p>
     *
     * @param positionCode 岗位对象
     * @param model
     * @return
     */
    @RequestMapping("/addToEditPosition")
    public String addToEditPosition(String positionCode, Model model) {
        PositionEntity posEntity = this.positionService.queryPosByCode(positionCode);
        return this.toEditPosition(posEntity.getPositionId(), model);
    }

    /**
     * <p>
     * Description: 删除岗位记录
     * </p>
     *
     * @param positionId 岗位对象
     * @param response
     */
    @RequestMapping("/deletePosition")
    public void deletePosition(Integer positionId, HttpServletResponse response) {
        int count = this.positionService.queryUserCountByPos(positionId);
        if (count < 1) {
            PositionEntity position = new PositionEntity();
            position.setPositionId(positionId);
            position.setUpdateUser(getSessionUser().getUserId());
            position.setUpdateTime(new Date());
            positionService.delete(position);
            log.info("用户" + getSessionUser().getUserName() + "删除了岗位:" + (new Gson()).toJson(position));
            super.ajaxJson(response, AjaxReturnVO.success());
        } else {
            super.ajaxJson(response, AjaxReturnVO.failed("该岗位正在被使用，不能删除！"));
        }
    }

    /**
     * 功能描述: <br>
     * 根据机构查询岗位(不包含该岗位自己)
     *
     * @param orgCode 机构代码
     * @param response 响应
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/getPositionByOrg")
    public void getPositionByOrg(String orgCode, String positionCode, HttpServletResponse response) {
        PositionEntity posInfo = new PositionEntity();
        posInfo.setOrgCode(orgCode);
        posInfo.setPositionCode(positionCode);
        List<PositionEntity> list = this.positionService.queryPositionListByOrg(posInfo);
        this.ajaxJson(response, list);
    }

    /**
     * 功能描述: <br>
     * 删除岗位与角色的关联关系
     *
     * @param positionCode
     * @param roleCode
     * @param response
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/deletePositionRole")
    public void deletePositionRole(String positionCode, String roleCode, HttpServletResponse response) {
        PositionRoleEntity posRole = new PositionRoleEntity();
        posRole.setPositionCode(positionCode);
        posRole.setRoleCode(roleCode);
        this.positionService.deletePositionRole(posRole);
        log.info("用户" + getSessionUser().getUserName() + "删除了岗位角色关系:" + (new Gson()).toJson(posRole));
        super.ajaxJson(response, AjaxReturnVO.success());
    }
}
